import { createSlice, createAsyncThunk, type PayloadAction } from "@reduxjs/toolkit";
import * as API from "../api/http";
import type { shopTS, typeTS } from "./crmsTS";

//rtk状态的ts类型
type initialStateTS = {
    shoplist: Array<shopTS>
    typelist: Array<typeTS>
    activeKey: string
}

let initialState: initialStateTS = { //初始状态 === 管理状态
    shoplist: [], //商品列表
    typelist: [], //分类列表
    activeKey: '1', //分类的高亮
}

//创建切片
const todosSlice = createSlice({
    name: "todos",
    initialState,
    reducers: { //同步修改状态的方法

        //商品列表数据
        t_getshoplist(state: initialStateTS, action: PayloadAction<Array<shopTS>>) { //获取商品列表
            state.shoplist = action.payload
        },

        //分类列表数据
        t_gettypelist(state: initialStateTS, action: PayloadAction<Array<typeTS>>) { //获取分类列表
            state.typelist = action.payload
        },

        //添加商品列表数据
        t_addshoplist(state: initialStateTS, action: PayloadAction<shopTS>) {
            state.shoplist.push(action.payload)
        },

        //编辑商品列表数据
        t_editshoplist(state: initialStateTS, action: PayloadAction<shopTS>) {
            let i = state.shoplist.findIndex(item => {
                return item.id == action.payload.id
            })
            state.shoplist[i] = action.payload
        },

        //删除商品列表数据
        t_delshoplist(state: initialStateTS, action: PayloadAction<number>) {
            state.shoplist = state.shoplist.filter(item => {
                return item.id !== action.payload
            })
        },

        //切换分类的高亮
        t_changeActivekey(state: initialStateTS, action: PayloadAction<string>) {
            state.activeKey = action.payload
        },

        //添加分类列表数据
        t_addtypelist(state: initialStateTS, action: PayloadAction<typeTS>) {
            state.typelist.push(action.payload)
        },

        //删除分类列表数据
        t_deltypelist(state: initialStateTS, action: PayloadAction<string>) {
            state.typelist = state.typelist.filter((item:typeTS) => {
                return item.key !== action.payload
            })
        }

    },
});

export const actionfn = todosSlice.actions;
export default todosSlice.reducer;

//删除分类列表数据
export let async_deltypelist = createAsyncThunk('async_deltypelist',
    async (key: string, store) => {
        //删除接口没有返回值
        await API.http_deltypelist(key)
        store.dispatch(actionfn.t_deltypelist(key))
    })

//添加分类列表数据
export let async_addtypelist = createAsyncThunk('async_addtypelist',
    async (obj: typeTS, store) => {
        //调取添加接口
        let res = await API.http_addtypelist(obj)
        //同步修改异步
        store.dispatch(actionfn.t_addtypelist(res))
    })

//搜索商品列表数据
export let async_searchshoplist = createAsyncThunk('async_searchshoplist',
    async (title: string, store) => {
        let res = await API.http_searchshoplist(title)
        store.dispatch(actionfn.t_getshoplist(res))
    })

//删除商品列表数据
export let async_delshoplist = createAsyncThunk('async_delshoplist',
    async (id: number, store) => {
        //删除接口没有返回值
        await API.http_delshoplist(id)
        store.dispatch(actionfn.t_delshoplist(id))
    })

//编辑商品列表数据 异步方法
export let async_editshoplist = createAsyncThunk('async_editshoplist',
    async (obj: shopTS, store) => {
        let res = await API.http_enitshoplist(obj)
        store.dispatch(actionfn.t_editshoplist(res))
    })

//添加商品列表数据 异步方法
export let async_addshoplist = createAsyncThunk('async_addshoplist',
    async (obj: shopTS, store) => {
        //调取添加接口
        let res = await API.http_addshoplist(obj)
        //同步修改异步
        store.dispatch(actionfn.t_addshoplist(res))
    })

//异步操作 获取商品列表
export let async_shoplist = createAsyncThunk('async_shoplist',
    async (_, store) => {
        //调取接口
        let res = await API.http_shoplist()
        //console.log(res);
        store.dispatch(actionfn.t_getshoplist(res))
    })

//获取分类列表数据
export let async_typelist = createAsyncThunk('async_typelist',
    async (_, store) => {
        //调取接口
        let res = await API.http_typelist()
        //同步修改异步
        store.dispatch(actionfn.t_gettypelist(res))
    })
